## 2008 - 2009 NES Panel Data 
## Levendusky/Stecula Cambridge Element 
## Original version: Summer 2020 
## This version: Winter 2021 

## This replicates Tables 1 and 2 from the Element 

## Setwd, load libraries and data 
library(tidyverse) 
library(haven)
library(estimatr) 
library(modelsummary)
#setwd("SET YOUR WORKING DATA HERE")
nes <- read_dta(file="anes2008_2009panel_dataset.dta") 

## Respondent's party ID 
## use baseline wave 1 party id 
nes <- nes %>%
  mutate(party_id = ifelse(der08w1 > -1,der08w1+1,NA),
         ## 3-point pid (1 = Dem, 2 = Pure Ind, 3 = Rep)
         pid = ifelse(party_id < 4,1,
                      ifelse(party_id == 4,2,3)), 
         dem = ifelse(party_id != 4, 
                      ifelse(party_id < 4,1,0),NA),
         party_strength = ifelse(party_id == 4,0,
                                 ifelse(party_id == 3 | party_id == 5,1,
                                        ifelse(party_id == 2 | party_id == 6,2,
                                               ifelse(party_id == 1 | party_id == 7,3,NA)))), 
         sp = ifelse(party_strength == 3,1,0),
         ideo = ifelse(der09w1 > 0,der09w1,NA),
         sorted = ifelse((pid == 1 & ideo < 4) | (pid == 3 & ideo > 4),1,0))

## Demographics (to predict this) 
nes <- nes %>%  
  mutate(female = ifelse(der01 == 2,1,0),
         ## create bins for age: 
         ## (1) 18 - 34, (2) 35 - 49, (3) 50 - 64, (4) 65+ 
         age = ifelse(der02 < 35,1,
                      ifelse(der02 > 34 & der02 < 50,2,
                             ifelse(der02 > 49 & der02 < 65,3,4))), 
         white = ifelse(der03a > -1,
                        ifelse(der03a == 1,1,0),NA),
         afam = ifelse(der03b > -1, 
                       ifelse(der03b == 1,1,0),NA),
         ## cat: (1) less than HS, (2) HS grad, (3) some college, (4) college + 
         educ = ifelse(der05 > 0, 
                       ifelse(der05==1,1,
                              ifelse(der05==2,2,
                                     ifelse(der05==3,3,4))),NA),
         baplus = ifelse(der05 > 0,
                         ifelse(der05 > 3,1,0),NA),
         ## income terciles: 
         ## less than 40K, 40K - 85K, 85K+ 
         income = ifelse(der06 > 0,
                         ifelse(der06 < 11,1,
                                ifelse(der06 > 10 & der06 < 15,2,
                                       ifelse(der06 > 14,3,NA))),NA), 
         ## median (above/below median) 
         inc_med = ifelse(der06 > 0, 
                          ifelse(der06 < 14,1,0),NA), 
         polint = ifelse(w9h1 > 0,(-1*w9h1)+6,NA), 
         ## 3 point interest 
         ## (1) not interested, (2) slightly/moderately interested, (3) very/extremely 
         polint_3 = ifelse(w9h1 > 0,
                           ifelse(w9h1 == 5,1,
                                  ifelse(w9h1 == 4 | w9h1 == 3,2,3)),NA))
                                  

## Political Knowledge (battery in w2) 
nes <- nes %>%
  ## get binary answers and then sum up 
  mutate(know_prez = ifelse(w2u2 > 0,
                            ifelse(w2u2 == 2,1,0),NA),
         know_sen = ifelse(w2u3 > 0,
                            ifelse(w2u3 == 6,1,0),NA),
         know_sen2 = ifelse(w2u4 > 0,
                            ifelse(w2u4 == 2,1,0),NA),
         know_house = ifelse(w2u5 > 0,
                            ifelse(w2u5 == 2,1,0),NA),
         know_succession = ifelse(w2u6 > 0,
                            ifelse(w2u6 == 3,1,0),NA),
         know_veto = ifelse(w2u7 > 0,
                            ifelse(w2u7 == 2,1,0),NA)) %>% 
  rowwise(.) %>% 
  mutate(pol_know = sum(c(know_prez,know_sen,know_sen2,know_house,
                           know_succession,know_veto),na.rm=T),  
  ## those not asked the battery get set to NA 
         pol_know = ifelse(w2u2 == -5,NA,pol_know)) %>% 
  ungroup(.) 

                           
## Social networks battery 
## NB: you only get the battery if you spoke to someone about politics in the past 6 months (83% of the sample) 
## Three key variables (for up to 3 people): closeness, similarity of views, same party 
nes <- nes %>% 
  mutate(close_p1 = ifelse(w9zd4_1 >0,(-1*w9zd4_1)+6,NA),
         close_p2 = ifelse(w9zd4_2 >0,(-1*w9zd4_2)+6,NA),
         close_p3 = ifelse(w9zd4_3 >0,(-1*w9zd4_3)+6,NA), 
         diff_p1 = ifelse(w9zd9_1 >0,(-1*w9zd9_1)+6,NA),
         diff_p2 = ifelse(w9zd9_2 >0,(-1*w9zd9_2)+6,NA),
         diff_p3 = ifelse(w9zd9_3 >0,(-1*w9zd9_3)+6,NA),
         ## just do 3-point party ID (make leaners partisans) 
         ## scale is 1 = Dem, 2 = Pure Ind, 3 = Rep 
         pid_p1 = ifelse(w9zd12_1 >0,
                         ifelse(w9zd12_1 == 1,1,
                                ifelse(w9zd12_1 == 2,3,2)),NA), 
         pid_p1 = ifelse(pid_p1 == 2, 
                         ifelse(w9zd16_1 == 1,1,
                                ifelse(w9zd16_1 == 2,3,2)),pid_p1),
         pid_p2 = ifelse(w9zd12_2 >0,
                         ifelse(w9zd12_2 == 1,1,
                                ifelse(w9zd12_2 == 2,3,2)),NA), 
         pid_p2 = ifelse(pid_p2 == 2, 
                         ifelse(w9zd16_2 == 1,1,
                                ifelse(w9zd16_2 == 2,3,2)),pid_p2),
         pid_p3 = ifelse(w9zd12_3 >0,
                         ifelse(w9zd12_3 == 1,1,
                                ifelse(w9zd12_3 == 2,3,2)),NA), 
         pid_p3 = ifelse(pid_p3 == 2, 
                         ifelse(w9zd16_3 == 1,1,
                                ifelse(w9zd16_3 == 2,3,2)),pid_p3), 
         ## do you have the same party ID as your discussant? 
         same_pid_p1 = ifelse(pid == pid_p1,1,0),
         same_pid_p2 = ifelse(pid == pid_p2,1,0),
         same_pid_p3 = ifelse(pid == pid_p3,1,0),
         ## do you name a discussant from the other party? 
         xparty_p1 = ifelse(pid == 1 & pid_p1 == 3,1,0),
         xparty_p1 = ifelse(xparty_p1 == 0,
                            ifelse((pid == 3 & pid_p1 == 1),1,0),xparty_p1),
         xparty_p2 = ifelse(pid == 1 & pid_p1 == 3,1,0),
         xparty_p2 = ifelse(xparty_p2 == 0,
                            ifelse((pid == 3 & pid_p1 == 1),1,0),xparty_p2),
         xparty_p3 = ifelse(pid == 1 & pid_p2 == 3,1,0),
         xparty_p3 = ifelse(xparty_p3 == 0,
                            ifelse((pid == 3 & pid_p3 == 1),1,0),xparty_p3)) 

## How many people do you name from the other party? 
nes <- nes %>% 
  rowwise(.) %>% 
  mutate(numb_xp = sum(c(xparty_p1,xparty_p2,xparty_p3),na.rm=T),
         numb_sp = sum(c(same_pid_p1,same_pid_p2,same_pid_p3),na.rm=T)) %>% 
  ungroup(.) 
## set to NA if you don't answer this series 
nes$numb_xp <- ifelse(is.na(nes$xparty_p1),NA,nes$numb_xp) 
nes$numb_sp <- ifelse(is.na(nes$same_pid_p1),NA,nes$numb_sp)
## have at least 1 cross-party discussant 
nes$atleastone <- ifelse(nes$numb_xp > 0,1,0) 

## What's the distribution? 
prop.table(table(nes$numb_xp)) 
#0          1          2          3 
#0.76346912 0.08935611 0.09329829 0.05387648 
## roughly one-quarter have some cross-party interaction! 
 
prop.table(table(nes$numb_sp))
#0         1         2         3 
#0.1836007 0.2263815 0.2655971 0.3244207 

## how close are people to someone from the other party? 
prop.table(table(nes$close_p1[nes$xparty_p1==1])) 
## 62% are "extremely close" or "very close" (only 9% are "not at all close")

#############################################
## Table 1: Who Speaks to the Other Party? ##
#############################################

## Who speaks to the other party? 
mean(nes$atleastone,na.rm=T)
tapply(nes$atleastone,nes$age,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$der04,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$baplus,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$income,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$polint_3,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$pid,function(x)mean(x,na.rm=T))
tapply(nes$atleastone,nes$sp,function(x)mean(x,na.rm=T))


#################################################
## Table 2: Effects of Cross-Party Discussion? ##
#################################################

## out-party sentiments (wave 11) 
## positive sentiments: 0 = none, 1 = slightly, 2 = moderate, 3 = very, 4 = extremely 
nes <- nes %>% 
  mutate(dem_pos = ifelse(w11lb2 > 0, 
                          ifelse(w11lb2 == 2,0,(-1*w11lb4)+5),NA),
         dem_neg = ifelse(w11lb3 > 0 & w11lb5 != -7, 
                          ifelse(w11lb3 == 2,0,(-1*w11lb5)+5),NA),
         rep_pos = ifelse(w11lb7 > 0, 
                          ifelse(w11lb7 == 2,0,(-1*w11lb9)+5),NA),
         rep_neg = ifelse(w11lb8 > 0 & w11lb10 != -7, 
                          ifelse(w11lb8 == 2,0,(-1*w11lb10)+5),NA),
         same_pos = ifelse(pid == 1,dem_pos,
                           ifelse(pid == 3,rep_pos,NA)),
         other_pos = ifelse(pid == 3,dem_pos,
                            ifelse(pid == 1,rep_pos,NA)),
         other_neg = ifelse(pid == 3,dem_neg,
                            ifelse(pid == 1,rep_neg,NA)),
         any_neg_other = ifelse(other_neg > 0,1,0),
         any_pos_other = ifelse(other_pos > 0,1,0))

## Is there an effect? 
summary(lm_robust(other_pos ~ numb_xp,
                  data = nes)) 
summary(lm_robust(any_pos_other ~ numb_xp,
                  data = nes)) 
tapply(nes$any_pos_other,nes$numb_xp,function(x)mean(x,na.rm=T)) 
# 0         1         2         3 
# 0.3547297 0.4117647 0.4705882 0.5000000 
## nice linear effect 

## any positive thoughts about the other party? 
m0 <- lm_robust(any_pos_other ~ atleastone,
                  data = nes) 
m1 <- lm_robust(any_pos_other ~ numb_xp,
                  data = nes) 
## if you have at least 1 cross-party discussant, you're 10 percentage points more likely to say something nice about the other party! 

## Output to a table 
models <- list()
models[["(1)"]] <- m0  
models[["(2)"]] <- m1 

msummary(models,
         coef_map = c("(Intercept)" = "Constant",
                      "atleastone" = "At Least One Cross-Party Discussant",
                      "numb_xp" = "Number of Cross-Party Discussants"),
         stars = T, 
         output =  "tables/table2_nes_panel.html")

